//
//  Team9s_aboutDiscussCell.m
//  Team9Project
//
//  Created by dlios on 15/10/16.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "Team9s_aboutDiscussCell.h"
#import "Team9s_discussModel.h"
#import <Masonry.h>
#import <UIImageView+WebCache.h>
@implementation Team9s_aboutDiscussCell



-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatAllView];
    }
    return self;
}



- (void)setDissModel:(Team9s_discussModel *)dissModel{
    if (_dissModel != dissModel) {
        _dissModel = dissModel;
    }
    
    
    // 描述
    self.describleLabel.text = self.dissModel.title;
    
    // 图片
    if (self.dissModel.pics.count ) {
        
        for (int i = 0; i < 4; i++) {
            UIImageView *imageView = self.imageViews[i];
            imageView.userInteractionEnabled = NO;
            imageView.image = nil;
            if (self.dissModel.pics.count > i) {
                imageView.userInteractionEnabled = YES;
                [imageView sd_setImageWithURL:[NSURL URLWithString:self.dissModel.pics[i][@"pic"]] placeholderImage:[UIImage imageNamed:@"111"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                    [UIView animateWithDuration:0.3 animations:^{
                        CATransition *transition = [CATransition animation];
                        transition.type = @"fade";
                        [transition setDuration:0.5];
                        [transition setRepeatCount:1];
                        // 向imageView上添加动画效果
                        [imageView.layer addAnimation:transition forKey:@"transition"];
                    }];
                }];
            }
        }

    }
    
    
    if (self.dissModel.pics.count) {
        UIImageView *lastView = nil;
        
        for (UIImageView *imageView in self.imageViews) {
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.describleLabel.mas_bottom).offset(10 / 375.0 * WIDTH);
                if (lastView) {
                    make.left.equalTo(lastView.mas_right).offset(1 / 375.0 * WIDTH);
                } else make.left.equalTo(self.contentView).offset(10 / 375.0 * WIDTH);
                make.width.equalTo(@((WIDTH - 3 / 375.0 * WIDTH - 40 / 375.0 * WIDTH)  / 4));
                make.height.equalTo(imageView.mas_width);;
            }];
            lastView = imageView;
        }
        
    }else{
        UIImageView *lastView = nil;
        for (UIImageView *imageView in self.imageViews) {
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.describleLabel.mas_bottom).offset(10 / 375.0 * WIDTH);
                if (lastView) {
                    make.left.equalTo(lastView.mas_right).offset(1 / 375.0 * WIDTH);
                } else make.left.equalTo(self.contentView).offset(10 / 375.0 * WIDTH);
                make.width.equalTo(@((WIDTH - 3 / 375.0 * WIDTH - 40 / 375.0 * WIDTH)  / 4));
                make.height.equalTo(@0);
            }];
            lastView = imageView;
        }
        
        
    }

    
    
    
    self.userNameLabel.text= self.dissModel.author_nick;
    NSDictionary *fontDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:11 / 375.0 * WIDTH], NSFontAttributeName, nil];
    CGRect rect = [self.userNameLabel.text boundingRectWithSize:CGSizeMake(0, 12 /  375.0 * WIDTH) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
    [self.userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(10 / 375.0 * WIDTH);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-10 / 375.0 * WIDTH);
        make.width.mas_equalTo(rect.size.width ).priorityHigh();
        make.height.mas_equalTo(12 / 375.0 * WIDTH);
        
    }];

    
    
    self.typeLabel.text = self.dissModel.class_name;
    self.respondImageView.image = [UIImage imageNamed:@"iconfont-message"];
    self.respondLabel.text = self.dissModel.reply_count;
    
    
}








-(void)creatAllView
{
    
    // 用户名
    self.userNameLabel = [[Team9BaseLabel alloc] initWith:LableColorTypeOrangeGreen];
    self.userNameLabel.font = [UIFont systemFontOfSize:11 / 375.0 * WIDTH];
    [self.contentView addSubview:self.userNameLabel];
  
    
    // 类型
    self.typeLabel = [[Team9BaseLabel alloc] init];
    self.typeLabel.font = [UIFont systemFontOfSize:11 / 375.0 * WIDTH];
    [self.contentView addSubview:self.typeLabel];
    self.typeLabel.backgroundColor = [UIColor clearColor];
    
    // 分割
    self.cutView = [[Team9BaseView alloc] init];
    [self.contentView addSubview:self.cutView];
    self.cutView.backgroundColor= [UIColor colorWithWhite:0.885 alpha:1.000];

    
    // 内容
    self.describleLabel = [[Team9BaseLabel alloc] init];
    self.describleLabel.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.describleLabel];
    self.describleLabel.numberOfLines = 0;
    [self.describleLabel sizeToFit];
    self.describleLabel.font = [UIFont systemFontOfSize:13 / 375.0 * WIDTH];
    
    // 讨论
    self.respondImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.respondImageView];
    self.respondImageView.backgroundColor = [UIColor clearColor];
    
    self.respondLabel = [[Team9BaseLabel alloc] init];
    [self.contentView addSubview:self.respondLabel];
    self.respondLabel.backgroundColor = [UIColor clearColor];
    self.respondLabel.font = [UIFont systemFontOfSize:10 / 375.0 * WIDTH];

    
    
    // 图片

    self.imageViews = @[].mutableCopy;
    for (int i = 0; i < 4; i++) {
        UIImageView *imageView = [UIImageView new];
        
        imageView.userInteractionEnabled = YES;
        
        
        // 添加手势
        // 设置点击手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [imageView addGestureRecognizer:tap];
        
        [self.imageViews addObject:imageView];
        self.imageViews[i].tag = 1000 + i;
        [self.contentView addSubview:imageView];
              
       
    }

    
    
    
    [self creat_Constraints];
    
    
}

#pragma mark
- (void)tapAction:(UITapGestureRecognizer *)tap{
  
      if (_block) {
        self.block(tap.view.tag);
    }
    


    
}

- (void)creat_Constraints{
    
    
    [self.describleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(10 / 375.0 * WIDTH);
        make.top.equalTo(self.contentView.mas_top).offset(10 / 375.0 * WIDTH);
        make.right.equalTo(self.contentView).offset(-10 / 375.0 * WIDTH);
        make.height.mas_equalTo(20 / 375.0 * WIDTH);
    }];
    
    
    
    
    
    
    [self.cutView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.userNameLabel.mas_right).offset(3 / 375.0 * WIDTH);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-10 / 375.0 * WIDTH);
        make.width.mas_equalTo(1 / 375.0 * WIDTH);
        make.height.equalTo(self.userNameLabel.mas_height);
        
    }];
    
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.cutView.mas_right).offset(3 / 375.0 * WIDTH);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-10 / 375.0 * WIDTH);
        make.width.equalTo(self.contentView.mas_width).multipliedBy(0.18);
        make.height.mas_equalTo(12 / 375.0 * WIDTH);
        
    }];
    
    [self.respondImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-10 / 375.0 * WIDTH);
        make.width.mas_equalTo(12 / 375.0 * WIDTH);
        make.height.mas_equalTo(12 / 375.0 * WIDTH);
        make.right.equalTo(self.respondLabel.mas_left).offset(-3 / 375.0 * WIDTH);
        
    }];
    
    [self.respondLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-10 / 375.0 * WIDTH);
        make.width.equalTo(self.contentView.mas_width).multipliedBy(0.05).priorityLow();
        make.height.equalTo(self.typeLabel.mas_height);
        make.right.equalTo(self.contentView.mas_right).offset(-10 / 375.0 * WIDTH);
        
    }];

    
    

    
    
}





-(void)layoutSubviews
{
    [super layoutSubviews];
    
}


@end
